package com.vtuzx.kingdee.xqc_core.dao;

import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import com.vtuzx.kingdee.xqc_core.constant.BillStatus;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.Date;

//吉客云自采品列表
@Component
public class SelfProductDao extends VtuzxDaoImpl {
    /**
     * 集合名称
     */
    public static final String NAME = "t_self_product";

    public SelfProductDao() {
        super(NAME);
    }

    public SelfProductDao(String collName) {
        super(collName);
    }

    /**
     * 新建单据
     *
     * @param _userId  用户ID
     * @param userName 用户名
     * @param doc      数据
     * @return 单据ID
     */
    public ObjectId insertBill(ObjectId _userId, String userName, Document doc) {
        doc.append("_createUserId", _userId);
        doc.append("createUserName", userName);
        doc.append("_updateUserId", _userId);
        doc.append("updateUserName", userName);
        return insertOne(doc);
    }

    /**
     * 审批单据
     *
     * @param _userId       用户ID
     * @param userName      用户名
     * @param selfProductNo 数据编号
     */
    public void audit(ObjectId _userId, String userName, String selfProductNo) {
        updateOne(Criteria.where("selfProductNo").is(selfProductNo),
                new Document("status", BillStatus.Audit.toString())
                        .append("auditTime", new Date())
                        .append("_auditUserId", _userId)
                        .append("auditUserName", userName));
    }

    /**
     * 反审批单据
     *
     * @param selfProductNo 数据编号
     */
    public void unaudit(String selfProductNo) {
        updateOne(Criteria.where("selfProductNo").is(selfProductNo), new Document("status", BillStatus.Save.toString()));
    }

    /**
     * 查找自采品编号
     *
     * @param selfProductNo 数据编号
     */
    public boolean isSelfProduct(String selfProductNo) {
        return count(null, Criteria.where("selfProductNo").is(selfProductNo)) > 0;
    }
}